import pojo.FaceDectet;
import pojo.Face_list;
import pojo.Landmark72;
import pojo.Location;

import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.awt.image.ImageObserver;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author: GaoXu
 * @Date: 2022/05/03/23:25
 * @Description:
 */
public class ImageService extends JFrame {
    public static void main(String[] args) throws IOException {
        String url = "C:\\Users\\GA666666\\Desktop\\ImageRepo\\4.png";
        FaceDectet faceDectet = FaceApi.faceDetcet(url);
        List<Face_list> face_list = faceDectet.getResult().getFace_list();
        System.out.println(face_list.get(0).toString());
        if (face_list.size() == 1) {
            new ImageService(face_list.get(0),url);
        }
    }

    public ImageService(Face_list face_list,String urlString) throws IOException {
        Location location = face_list.getLocation();
        List<Landmark72> landmark72 = face_list.getLandmark72();

        JPanel panel = new JPanel(new BorderLayout());
        ImageIcon imageIcon = new ImageIcon(urlString);

        File inputStream = new File(urlString);


        BufferedImage bufferedImage = ImageIO.read(inputStream);

        drawPoints(face_list, bufferedImage);


        Graphics2D print = bufferedImage.createGraphics();

        BufferedImage icon_label = ImageIO.read(new FileInputStream(new File("C:\\Users\\GA666666\\Desktop\\ImageRepo\\mask.png")));
        print.rotate(Math.toRadians(face_list.getAngle().getRoll()),landmark72.get(57).getX(),landmark72.get(57).getY());
        print.drawImage(icon_label, (int) face_list.getLandmark72().get(0).getX(), (int) face_list.getLandmark72().get(0).getY(), (int)(landmark72.get(12).getX()-landmark72.get(0).getX()),  (int)(landmark72.get(6).getY()-landmark72.get(0).getY()), null);


        Image i = bufferedImage;


        ImageIcon iIcon = new ImageIcon(i);

        int iconHeight = imageIcon.getIconHeight();
        int iconWidth = imageIcon.getIconWidth();
        JLabel label = new JLabel(iIcon);
        panel.add(label, BorderLayout.CENTER);
        this.setContentPane(panel);

        this.setSize(iconWidth, iconHeight);

        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        this.setTitle("显示图像");

        this.setVisible(true);

    }

    public BufferedImage drawPoints(Face_list face_list, BufferedImage bufferedImage) {
        Graphics2D print = bufferedImage.createGraphics();
        print.setColor(Color.red);
        List<Landmark72> landmark72 = face_list.getLandmark72();
        landmark72.stream().forEach(landmark -> {
            print.drawRoundRect((int) landmark.getX(), (int) landmark.getY(), 5, 5, 5, 5);
        });
        return bufferedImage;
    }

}
